<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<link rel="stylesheet" href="../../lib/layui/css/layui.css"/>
<script src="../../lib/layui/layui.js"></script>
<script src="../../js/jquery.min.js"></script>
<script type="text/javascript" src="../../js/xadmin.js"></script>
<script src="../../js/common.js"></script>
<body>
<!-- 添加院内协定方模板 -->
<form class="layui-form layui-form-pane" action="">
    <div class="layui-form-item layui-form-text">
        <div class="layui-form-item">
            <label class="layui-form-label">协定方名称</label>
            <div class="layui-input-block">
                <input type="text" name="t_name" placeholder="请输入协定方的名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">功效</label>
            <div class="layui-input-block">
                <input type="text" name="t_effect" placeholder="请输入协定方的功效" autocomplete="off" class="layui-input">
                <input type="hidden" name="templateNamber" autocomplete="off" class="layui-input" value="41">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label ">用法选择</label>
            <div class="layui-input-inline" >
                <select name="t_usage"  lay-verify="required" lay-search="" >
                    <option value="0" id="search" >直接选择或搜索选择</option>

                </select>
            </div>
        </div>
    </div>
    <!-- 穿梭框 -->
    <div id="test4" class="demo-transfer " style="margin-left:150px;"></div>
    <button type="button" style="margin-left:342px;" class="layui-btn layui-btn-warm layui-btn-radius" lay-template="getData1" >保存药品</button>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="add" style="margin-left:150px;" >立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary" style="margin-left:80px;">重置</button>
        </div>
    </div>
</form>
</body>
</html>

<!-- 穿梭框 -->
<script>
    layui.use(['transfer', 'layer', 'util','element', 'form'], function () {
        var $ = layui.$
            , transfer = layui.transfer
            , layer = layui.layer
            , form = layui.form
            , element = layui.element
            , util = layui.util;

        //穿梭框获取数据
        useAjax("/JT90/getDrugs",null,"JSON","GET",function(map){
            if (map.success){
                var data = map.result;
                transfer.render({
                    elem: '#test4'
                    , data: data
                    , title: ['搜索到的药品', '已添加的药品']
                    , showSearch: true
                    , height: 300
                    , id: 'drugs'
                    ,parseData: function(list) {
                        return {
                            "value": list.drId //数据值
                            , "title": list.drName //数据标题
                        }
                    }
                })
            }else{
                layer.msg(map.message);
            }
        })

        //用法下拉列表获取数据
        useAjax("/JT90/getUsage",null,"JSON","GET",function(map){
            $.each(map.result, function (index, item) {
                $('#search').after("<option value='"+item.usageId+"'>"+item.usageName+"</option>");
            });
            form.render();
        })


        //检验药品是否提交
        var check = 0;
        util.event('lay-template', {
            getData1: function () {
                var getData = transfer.getData('drugs'); //获取右侧数据
                if (getData != "" && getData != null) {
                    var dat = JSON.stringify(getData);
                    //发送ajax将数据给后台
                    useAjax("/JT90/saveDrugsInfo", dat, "JSON", "POST", function (count) {
                        if (count>0){
                            layer.msg("药品保存成功",{icon: 6});
                            check=count;
                        }else{
                            layer.msg("药品保存失败",{icon: 5});
                        }

                    })
                }
            }
        });


        //监听提交
        form.on('submit(add)',
            function (data) {
                if (check == 0){
                    layer.alert("药品未提交，请确认药品后提交!")
                }else {
                    //发异步，把数据提交给php
                    useAjax("/JT90/AgreementPartyTemplate",data.field,"JSON","GET",function(map){
                        if (map.success){
                            layer.alert("增加成功", {icon: 6})
                            //关闭当前frame
                            xadmin.close();
                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        }else{
                            layer.msg(map.message);
                        }

                    })
                }
                return false;
            });
    })
</script>
